home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / linux / atari / source / source.lzh / atari-linux-0.01pl3 / fs / proc / root.c < prev   
Encoding:
C/C++ Source or Header  |  1994-06-05  |  3.6 KB  |  178 lines

  1. /*
  2.  *  linux/fs/proc/root.c
  3.  *
  4.  *  Copyright (C) 1991, 1992 Linus Torvalds
  5.  *
  6.  *  proc root directory handling functions
  7.  */
  8.  
  9. #include <asm/segment.h>
  10.  
  11. #include <linux/errno.h>
  12. #include <linux/sched.h>
  13. #include <linux/proc_fs.h>
  14. #include <linux/stat.h>
  15. #include <linux/config.h>
  16.  
  17. static int proc_readroot(struct inode *, struct file *, struct dirent *, int);
  18. static int proc_lookuproot(struct inode *,const char *,int,struct inode **);
  19.  
  20. static struct file_operations proc_root_operations = {
  21.     NULL,            /* lseek - default */
  22.     NULL,            /* read - bad */
  23.     NULL,            /* write - bad */
  24.     proc_readroot,        /* readdir */
  25.     NULL,            /* select - default */
  26.     NULL,            /* ioctl - default */
  27.     NULL,            /* mmap */
  28.     NULL,            /* no special open code */
  29.     NULL,            /* no special release code */
  30.     NULL            /* no fsync */
  31. };
  32.  
  33. /*
  34.  * proc directories can do almost nothing..
  35.  */
  36. struct inode_operations proc_root_inode_operations = {
  37.     &proc_root_operations,    /* default base directory file-ops */
  38.     NULL,            /* create */
  39.     proc_lookuproot,    /* lookup */
  40.     NULL,            /* link */
  41.     NULL,            /* unlink */
  42.     NULL,            /* symlink */
  43.     NULL,            /* mkdir */
  44.     NULL,            /* rmdir */
  45.     NULL,            /* mknod */
  46.     NULL,            /* rename */
  47.     NULL,            /* readlink */
  48.     NULL,            /* follow_link */
  49.     NULL,            /* bmap */
  50.     NULL,            /* truncate */
  51.     NULL            /* permission */
  52. };
  53.  
  54. static struct proc_dir_entry root_dir[] = {
  55.     { 1,1,"." },
  56.     { 1,2,".." },
  57.     { 2,7,"loadavg" },
  58.     { 3,6,"uptime" },
  59.     { 4,7,"meminfo" },
  60.     { 5,4,"kmsg" },
  61.     { 6,7,"version" },
  62.     { 7,4,"self" },    /* will change inode # */
  63.     { 8,3,"net" },
  64. #ifdef CONFIG_DEBUG_MALLOC
  65.     {13,6,"malloc" },
  66. #endif
  67.     {14,5,"kcore" },
  68. };
  69.  
  70. #define NR_ROOT_DIRENTRY ((sizeof (root_dir))/(sizeof (root_dir[0])))
  71.  
  72. static int proc_lookuproot(struct inode * dir,const char * name, int len,
  73.     struct inode ** result)
  74. {
  75.     unsigned int pid, c;
  76.     int i, ino;
  77.  
  78.     *result = NULL;
  79.     if (!dir)
  80.         return -ENOENT;
  81.     if (!S_ISDIR(dir->i_mode)) {
  82.         iput(dir);
  83.         return -ENOENT;
  84.     }
  85.     i = NR_ROOT_DIRENTRY;
  86.     while (i-- > 0 && !proc_match(len,name,root_dir+i))
  87.         /* nothing */;
  88.     if (i >= 0) {
  89.         ino = root_dir[i].low_ino;
  90.         if (ino == 1) {
  91.             *result = dir;
  92.             return 0;
  93.         }
  94.         if (ino == 7) /* self modifying inode ... */
  95.             ino = (current->pid << 16) + 2;
  96.     } else {
  97.         pid = 0;
  98.         while (len-- > 0) {
  99.             c = *name - '0';
  100.             name++;
  101.             if (c > 9) {
  102.                 pid = 0;
  103.                 break;
  104.             }
  105.             pid *= 10;
  106.             pid += c;
  107.             if (pid & 0xffff0000) {
  108.                 pid = 0;
  109.                 break;
  110.             }
  111.         }
  112.         for (i = 0 ; i < NR_TASKS ; i++)
  113.             if (task[i] && task[i]->pid == pid)
  114.                 break;
  115.         if (!pid || i >= NR_TASKS) {
  116.             iput(dir);
  117.             return -ENOENT;
  118.         }
  119.         ino = (pid << 16) + 2;
  120.     }
  121.     if (!(*result = iget(dir->i_sb,ino))) {
  122.         iput(dir);
  123.         return -ENOENT;
  124.     }
  125.     iput(dir);
  126.     return 0;
  127. }
  128.  
  129. static int proc_readroot(struct inode * inode, struct file * filp,
  130.     struct dirent * dirent, int count)
  131. {
  132.     struct task_struct * p;
  133.     unsigned int nr,pid;
  134.     int i,j;
  135.  
  136.     if (!inode || !S_ISDIR(inode->i_mode))
  137.         return -EBADF;
  138. repeat:
  139.     nr = filp->f_pos;
  140.     if (nr < NR_ROOT_DIRENTRY) {
  141.         struct proc_dir_entry * de = root_dir + nr;
  142.  
  143.         filp->f_pos++;
  144.         i = de->namelen;
  145.         put_fs_long(de->low_ino, &dirent->d_ino);
  146.         put_fs_word(i,&dirent->d_reclen);
  147.         put_fs_byte(0,i+dirent->d_name);
  148.         j = i;
  149.         while (i--)
  150.             put_fs_byte(de->name[i], i+dirent->d_name);
  151.         return j;
  152.     }
  153.     nr -= NR_ROOT_DIRENTRY;
  154.     if (nr >= NR_TASKS)
  155.         return 0;
  156.     filp->f_pos++;
  157.     p = task[nr];
  158.     if (!p || !(pid = p->pid))
  159.         goto repeat;
  160.     if (pid & 0xffff0000)
  161.         goto repeat;
  162.     j = 10;
  163.     i = 1;
  164.     while (pid >= j) {
  165.         j *= 10;
  166.         i++;
  167.     }
  168.     j = i;
  169.     put_fs_long((pid << 16)+2, &dirent->d_ino);
  170.     put_fs_word(i, &dirent->d_reclen);
  171.     put_fs_byte(0, i+dirent->d_name);
  172.     while (i--) {
  173.         put_fs_byte('0'+(pid % 10), i+dirent->d_name);
  174.         pid /= 10;
  175.     }
  176.     return j;
  177. }
  178.